package okhttp3.internal.huc;

import java.io.IOException;

import okhttp3.Request;
import okio.Buffer;
import okio.BufferedSink;

/**
 * <p>Title: BufferedRequestBody </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/11/21 16:33
 */
final class BufferedRequestBody extends OutputStreamRequestBody {
    final Buffer buffer = new Buffer();
    long contentLength = -1L;

    BufferedRequestBody(long expectedContentLength) {
        this.initOutputStream(this.buffer, expectedContentLength);
    }

    public long contentLength() throws IOException {
        return this.contentLength;
    }

    public Request prepareToSendRequest(Request request) throws IOException {
        if (request.header("Content-Length") != null) {
            return request;
        } else {
            this.outputStream().close();
            this.contentLength = this.buffer.size();
            return request.newBuilder().removeHeader("Transfer-Encoding").header("Content-Length", Long.toString(this.buffer.size())).build();
        }
    }

    public void writeTo(BufferedSink sink) throws IOException {
        this.buffer.copyTo(sink.buffer(), 0L, this.buffer.size());
    }
}
